<template>
  <el-container>
    <el-header style="height: 47px;">
      <p>泊位信息列表</p>
    </el-header>
    <el-main>
      <el-row class="btnGroup">
        <el-button class="twoBtn" @click="addBerth"><span>添加</span><i class="el-icon-plus"></i></el-button>
        <!--<el-button icon="el-icon-edit" @click="editBerth">编辑</el-button>-->
        <el-button class="twoBtn" @click="delBerth"><span>删除</span><i class="el-icon-delete"></i></el-button>
        <!--<el-button icon="el-icon-download">导出EXCEL</el-button>-->
      </el-row>
      <el-table
        :data="tableData.list"
        style="width:100%"
        height="600"
        ref="multipleTable"
        tooltip-effect="dark"
        stripe
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column fixed align="center" type="selection" width="55"></el-table-column>
        <el-table-column fixed sortable align="center"  prop="place_parkname" label="所在停车场" width="180"></el-table-column>
        <el-table-column sortable align="center" prop="place_id" label="泊位编号" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_chargeunitname" label="所在路段" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_no" label="路段内编号" width="170"></el-table-column>
        <el-table-column sortable align="center" prop="place_sfdwname" label="收费单位" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_typename" label="泊位类型" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_statusname" label="泊位状态" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_car" label="在停车辆" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_long" label="经度" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_lati" label="纬度" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_size" label="泊位尺寸" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_dc" label="地磁设备" width="150"></el-table-column>
        <el-table-column sortable align="center" prop="place_remark" label="备注" width="150"></el-table-column>
        <el-table-column fixed="right" align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button class="downBtn" @click="changeRow(scope.row)">
                <i class="el-icon-arrow-down"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit">编辑</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="total,sizes,prev,pager,next,jumper"
        :total="dataCount"
        :page-sizes="[10,20,30,50]"
        :page-size="dataPageSize"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        style="margin-top: 16px;"
      ></el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import { delBerth } from '../../../api/parkingResource/parkingSpace'
  import { getNum } from '../../../api/public/searchUnit'

  export default {
    name: 'parkingSpaceTable',
    props: ['tableLoad', 'tableData'],
    data() {
      return {
        changeVal: '',
        dataCount: 0,
        dataPageSize: 10,
        multipleTable: [],
        loading: false,
        pageNum: 0
      }
    },
    watch: {
      tableLoad: function(val) {
        this.loading = val
      },
      tableData: function(val) {
        this.dataCount = val.count
        this.dataPageSize = val.pageSize
      }
    },
    methods: {
      addBerth: function() {
        this.$emit('addBerth', true)
      },
      editBerth: function() {
        if (this.multipleTable.length !== 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning'
          })
          return
        }
        this.$emit('editBerth', [this.multipleTable[0], true])
      },
      delBerth: function() {
        if (this.multipleTable.length < 1) {
          this.$message({
            message: '请选择至少一条记录',
            type: 'warning'
          })
          return
        }
        this.$confirm('此操作将永久删除选中的记录,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          let delData = []
          for (var i = 0; i < this.multipleTable.length; i++) {
            delData.push(this.multipleTable[i].place_id)
          }
          delBerth(delData).then(res => {
            this.$emit('delSearch', getNum())
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleSelectionChange: function(val) {
        this.multipleTable = val
      },
      handleCurrentChange: function(val) {
        this.$emit('pageNum', val)
      },
      handleSizeChange: function(val) {
        this.$emit('pageSize', val)
      },
      handleCommand(val) {
        if (val === 'edit') {
          this.$emit('editBerth', [this.changeVal, true])
        } else {
          this.$confirm('此操作将永久删除选中的记录,是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let delData = []
            delData.push(this.changeVal.place_id)
            delBerth(delData).then(res => {
              this.$emit('delSearch', getNum())
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      changeRow(val) {
        this.changeVal = Object.assign({}, val) // copy obj
      }
    }
  }
</script>

<style scoped>
  .el-dropdown{
    width: 33px;
    height: 19px;
  }
  .el-dropdown > .el-button{
    width: 33px;
    height: 19px;
    padding: 3px 9px;
  }
  .el-main{
    padding: 0;
    margin-top: 24px;
    margin-left: 24px;
  }
  .el-header p{
    font-family: MicrosoftYaHei;
    font-size: 13px;
    margin-top: 22px;
    color: #7F8CA6;
  }
  .el-header{
    padding:0;
    margin-left: 24px;
    margin-right: 24px;
    border-bottom: 1px solid #E1E1E1;
  }
  .btnGroup{
  }
  .btnGroup > .twoBtn{
    width: 64px;
    height:24px;
    padding: 4px 20px;
    border: 1px solid #409eff;
    font-family: MicrosoftYaHei;
    color: #fff;
    font-size: 12px;
  }
  .btnGroup > .fourBtn{
    width: 64px;
    height:24px;
    padding: 4px 8px;
    border: 1px solid #409eff;
    font-family: MicrosoftYaHei;
    color: #fff;
    font-size: 12px;
  }
  .btnGroup > .el-button span{
    display: none;
  }
  .btnGroup > .el-button:hover{
    background-color: #409EFF;
  }
  .btnGroup > .el-button:hover span{
    display: block;
  }
  .btnGroup > .el-button:hover i{
    display: none;
  }
  .el-button i{
    color: #409eff;
  }
  .el-footer{
    text-align: right;
  }
</style>
